import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

// https://vite.dev/config/
export default defineConfig({
  server: {
    port: 8880,
    host: '0.0.0.0',
    cors: true, //为开发服务器配置 CORS , 默认启用并允许任何源
    open: true, //是否自动打开浏览器
    // 代理API
    proxy: {
      '/mh': {
        target: 'http://121.41.178.95:3007/mh',
        // target: 'http://127.0.0.1:3007/mh',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/mh/, ''),
      },
      '/uploads': {
        target: 'http://121.41.178.95:3007/uploads',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/uploads/, ''),
      },
    },
  },
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
})
